//
//  DSDengViewController.m
//  DS爱信
//
//  Created by 秦建英 on 14-6-1.
//  Copyright (c) 2014年 秦建英. All rights reserved.
//

#import "DSDengViewController.h"
#import "DSViewController.h"
#import "DSLgViewController.h"
#import "NetAccess.h"
@interface DSDengViewController ()<UITextFieldDelegate>

@end

@implementation DSDengViewController
{
    UITextField *_field1;
    UITextField *_field2;
    NSString *_string;
    NSMutableData *_data;
    NSUserDefaults *users;
    UIAlertView *_alte;
    UIButton *btn;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
           }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mainicon.png"]];
    imageView.frame =CGRectMake(40, 50,260, 110);
    [self.view addSubview:imageView];
    _field1 =[[UITextField alloc]initWithFrame:CGRectMake(30,imageView.frame.size.height+60, 320-70,35)];
    _field1.borderStyle =UITextBorderStyleLine;
    _field1.keyboardType =UIKeyboardTypeNumberPad;
    _field1.placeholder = @"请输入手机号:";
    [self.view addSubview:_field1];
    
    _field2 =[[UITextField alloc]initWithFrame:CGRectMake(30,imageView.frame.size.height+50+60, 320-70,35)];
    _field2.borderStyle =UITextBorderStyleLine;
    _field2.secureTextEntry =YES;
    _field2.delegate =self;
    _field2.placeholder = @"请输入密码:";
    [self.view addSubview:_field2];
    
    btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn .frame =CGRectMake(160, _field2.frame.origin.y+80,_field2.frame.size.width/2 , 35);
    btn.tag =1001;
    btn.backgroundColor =[UIColor colorWithRed:0.2f green:0.6f blue:0.9f alpha:1];
    [btn setTitle:@"立即登录" forState:UIControlStateNormal];
    btn.layer.cornerRadius =10;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *btn1 =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1 .frame =CGRectMake(30, btn.frame.origin.y, _field2.frame.size.width/2, 35);
    btn1.tag =1002;
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn1.backgroundColor =[UIColor colorWithRed:0.2f green:0.6f blue:0.9f alpha:1];
    [btn1 setTitle:@"注册" forState:UIControlStateNormal];
    btn1.layer.cornerRadius =10;
    [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    NSString *title=@"对不起,网络受限";
    NSString *message=@"请返回重新登录..";
    _alte =[[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
}
-(void)btnClick:(UIButton *)bbt
{
    if(bbt.tag==1001)
    {
        NetAccess *net = [NetAccess sharedNetAccess];
        if(![net haveNet])
        {
            NSLog(@"网络不可用");
            //没有网络
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"无网络连接" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }else{
            NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"http://182.92.157.109/api/mapp/login.aspx?a=%@&b=%@&s=0",_field1.text,_field2.text]];
            NSURLRequest *request =[NSURLRequest requestWithURL:url];
            NSURLConnection *connent =[[NSURLConnection alloc]initWithRequest:request delegate:self];
            [connent start];
        }
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }
}
#pragma mark--网络代理方法
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    _data =[[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    _string =[[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
    NSLog(@"---%@",_string);
    if([_string hasPrefix:@"0|1"])
    {
        NSString * message =@"登陆成功,请稍后..";
        NSString *title =@"恭喜-亲！";
        _alte.message =message;
        _alte.title =title;
        [_alte show];
        DSViewController *list =[[DSViewController alloc]init];
        NSMutableArray *_usert =[NSMutableArray array];
        [_usert removeAllObjects];
        users =[NSUserDefaults standardUserDefaults];
        NSMutableDictionary *dict =[[NSMutableDictionary alloc]init];
        [dict setObject:_field1.text forKey:@"username"];
        [dict setObject:_field2.text forKey:@"userword"];
        [_usert addObject:dict];
        [users setObject:_usert forKey:@"users"];
        [users synchronize];
        [self presentViewController:list animated:YES completion:^{
        }];
    }
    else
    {
        NSString * message =_string;
        NSString *title =@"登陆失败！";
        _alte.message =message;
        _alte.title =title;
        [_alte show];
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_field2 resignFirstResponder];
    [_field1 resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
